package client;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import msg.*;
import gui.*;

/**
 *
 * @author tomek
 */
public class UsersList {
    
    static Vector<User> users = new Vector<User>();
    private static gui.UsersListGui myGui;
    private static WaitingForMsg waiting;



    public UsersList() {
        users = loadUsersList();
        myGui = new UsersListGui(this, users);
  
        waiting = new WaitingForMsg();
        waiting.start();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                myGui.showGui();
            }
        });
    }




    public static void main(String[] args) {

        System.out.println("aaa");
        

    }
    
    public Vector getUsers() { return users; }
    
    public void saveUsersList(Vector<User> users) {
        DataOutputStream dataOut;
        
        try {
            dataOut = new DataOutputStream(new FileOutputStream("contacts"));
        }
        catch(IOException exc) {
            JOptionPane.showMessageDialog(new JFrame(), "Nie udalo sie utworzyc pliku listy uzytkownikow.", "Blad", JOptionPane.ERROR_MESSAGE);
            System.out.println("Nie mozna otworzyc pliku.");
            return;
        }

        try {
            for (int i = 0; i < users.size(); i++) {
                dataOut.writeBytes(users.get(i).getFullDetails()+"\n");
            }
            dataOut.writeBytes("EOF\n");
        }
        catch(IOException exc) {
            System.out.println("Blad przy wpisywaniu.");
        }
        try {
            dataOut.close();
        }
        catch (IOException e) {
            System.out.println("Blad przy zamykaniu.");
        }
    }
    
    public static Vector<User> loadUsersList() {
        
        BufferedReader br;
        String line;
        Vector<User> usersVector = new Vector<User>();
        
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("contacts")));
            if (br != null) System.out.print("Otworzylem kontakty... ");
        }
        catch (IOException exc) {
            System.out.println("Blad otwarcia pliku.");
            return null;
        }

        try {
            while(!((line = br.readLine()).equals("EOF"))) {
                int i = 0, j = 0;
                char c;
                String[] tmp = {"","","","","",""};
                
                while((c=line.charAt(i)) != '$') {
                    if(c == ';') j++;
                    else {tmp[j]=tmp[j].concat(c+"");}
                    i++;
                    
                }
//                for (int k = 0; k < 6; k++ ) System.out.print(tmp[k]);
                
                usersVector.add(new User(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]));
                
            }
            System.out.println("Koniec pliku");
            
        }
        catch(IOException exc) {
            System.out.println("Blad odczytu.");
        }
        try {
            br.close();
        }
        catch (IOException e) {
            System.out.println("Blad przy zamykaniu.");
        }
        
        return usersVector;
   }
    
    static class WaitingForMsg extends Thread {
        public void run() {
            while (true) {
                Connection c = new Connection(2000);
                MsgContent m = c.receiveMsg();
                m.display();
                
                String u = m.getSender();
                Conversation conv;
            
                if(myGui.getConvsTabel().containsKey(u)) {
                    conv = (Conversation)myGui.getConvsTabel().get(u);
                    conv.getGui().setVisible(true);
                }
                else {
                    myGui.getConvsTabel().put(u, new Conversation(u));
                }
                
            }
        }
    }

}
